package com.ruoyi.project.system.news.service;

import java.util.Date;
import java.util.List;

import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.news.mapper.NewsMapper;
import com.ruoyi.project.system.news.domain.News;
import com.ruoyi.project.system.news.service.INewsService;
import com.ruoyi.common.utils.text.Convert;

import static com.ruoyi.common.utils.security.ShiroUtils.getSysUser;
import static com.ruoyi.common.utils.security.ShiroUtils.getUserId;

/**
 * 校园新闻 服务层实现
 * 
 * @author Waynnn
 * @date 2019-04-04
 */
@Service
public class NewsServiceImpl implements INewsService 
{
	@Autowired
	private NewsMapper newsMapper;

	@Autowired
	private IDeptService deptService;

	/**
     * 查询校园新闻信息
     * 
     * @param newsId 校园新闻ID
     * @return 校园新闻信息
     */
    @Override
	public News selectNewsById(Long newsId)
	{
	    return newsMapper.selectNewsById(newsId);
	}
	
	/**
     * 查询校园新闻列表
     * 
     * @param news 校园新闻信息
     * @return 校园新闻集合
     */
	@Override
	public List<News> selectNewsList(News news)
	{
		List<News> newsList=newsMapper.selectNewsList(news);
		for (News news1:newsList){
			news1.setDept(deptService.findSchoolObjByDeptId(news1.getDeptId()));
		}
	    return newsList;
	}
	
    /**
     * 新增校园新闻
     * 
     * @param news 校园新闻信息
     * @return 结果
     */
	@Override
	public int insertNews(News news)
	{
		User user=getSysUser();
		news.setDeptId(deptService.findSchoolObjByDeptId(user.getDeptId()).getDeptId());
		news.setCreateBy(user.getUserName());
		news.setCreateTime(new Date());
	    return newsMapper.insertNews(news);
	}
	
	/**
     * 修改校园新闻
     * 
     * @param news 校园新闻信息
     * @return 结果
     */
	@Override
	public int updateNews(News news)
	{
		User user=getSysUser();
		news.setDeptId(deptService.findSchoolObjByDeptId(user.getDeptId()).getDeptId());
		news.setUpdateBy(user.getUserName());
		news.setUpdateTime(new Date());
	    return newsMapper.updateNews(news);
	}

	/**
     * 删除校园新闻对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteNewsByIds(String ids)
	{
		return newsMapper.deleteNewsByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<News> selectNewsListByDeptId(Long deptId) {
		return newsMapper.selectNewsListByDeptId(deptId);
	}


}
